The Touch Controller (attached to the Gameboard node) exposes all the touch features available to creators. This allows creators to get information on the objects touching the board.

The Gameboard node is always available in your game as long as the SDK has been integrated. In order to access it we just need to find it.

    var gameboard = GetNode("/root/GameboardSDK") as GameboardPlugin;

With access to our Gameboard node we can then move on to obtain a reference to the Touch Controller. This is the controller that handles all the functions related to touch.

We will first need to define a reference to the controller to make it available throughout our script:

     TouchController touchController;

With our listener defined, we can now proceed to listen for touch events, captured as shapes. In our case we'll have functions called OnShapeFound, OnShapeUpdated, OnShapeLost, and AllShapesLost to handle these events.

    touchController = gameboard.GetNode("TouchController") as TouchController;

    touchController.OnShapeFound += OnShapeFound;
    touchController.OnShapeUpdated += OnShapeUpdated;
    touchController.OnShapeLost += OnShapeLost;
    touchController.AllShapesLost += AllShapesLost;

Now that we are able to receive the touch events, all that remains is to handle them in the game.

    void OnShapeFound(GameboardShape shape)
    {
        //Handle shape found
    }

    void OnShapeUpdated(GameboardShape shape)
    {
        //Handle shape updated
    }

    void OnShapeLost(GameboardShape shape)
    {
        //Handle shape lost
    }

    void AllShapesLost()
    {
        //Handle all shapes lost (no touch on the board)
    }

GameboardShape Properties

ShapeTypes

You can get specific shapes based on their id, or get all of the currently active shapes on the board.

In this example, imagine 123 is the current sessionId for a specific shape.

    GameboardShape shape = touchController.GetShape(123);

You can also get all of the current shapes touching the board:

    List<GameboardShape> shapes = touchController.GetShapes();

You can also get the cotour information for the shapes that are on the board. This can be used to draw an outline around the shapes of the board, or to potentially create detailed collision meshes.

NOTE: The skipContourLogic flag is true by default on the Gameboard node object. That means the extra logic to process the contours will not be done. If you want to use contours, make sure that flag is set to false.

Step-by-Step

Although we are working in a managed environment it is always a good idea to clean the listeners when no longer needed.

    public override void _ExitTree()
    {
        touchController.OnShapeFound -= OnShapeFound;
        touchController.OnShapeUpdated -= OnShapeUpdated;
        touchController.OnShapeLost -= OnShapeLost;
        touchController.AllShapesLost -= AllShapesLost;
    }

This section include the entire code in one single, easy to copy section.

    TouchController touchController;

    // Called when the node enters the scene tree for the first time.
    public override void _Ready()
    {
        var gameboard = GetNode("/root/GameboardSDK") as GameboardPlugin;
        touchController = gameboard.GetNode("TouchController") as TouchController;

        touchController.OnShapeFound += OnShapeFound;
        touchController.OnShapeUpdated += OnShapeUpdated;
        touchController.OnShapeLost += OnShapeLost;
        touchController.AllShapesLost += AllShapesLost;
    }

    void OnShapeFound(GameboardShape shape)
    {
        //Handle shape found
    }

    void OnShapeUpdated(GameboardShape shape)
    {
        //Handle shape updated
    }

    void OnShapeLost(GameboardShape shape)
    {
        //Handle shape lost
    }

    void AllShapesLost()
    {
        //Handle all shapes lost (no touch on the board)
    }

    public override void _ExitTree()
    {
        touchController.OnShapeFound -= OnShapeFound;
        touchController.OnShapeUpdated -= OnShapeUpdated;
        touchController.OnShapeLost -= OnShapeLost;
        touchController.AllShapesLost -= AllShapesLost;
    }